package com.itheima.controller;

import com.itheima.po.Book;
import com.itheima.po.Code;
import com.itheima.po.Result;
import com.itheima.po.ResultMap;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Andy
 * @Description //TODO BookController
 * @Date 2022/2/28 4:33 下午
 * @Version 1.0
 **/
//@Controller
@RequestMapping("/books")
@RestController
public class BookController {

    @Autowired
    private BookService bookService;

    //    post请求
//    RESTful增加 则URL仅仅是模块名 用什么请求方式做区分 POST
//    @RequestMapping(value = "", method = RequestMethod.POST)
    @PostMapping("")
    @ResponseBody
    public Result insert(@RequestBody Book book) {
        System.out.println("method POST URL /books");
        try{

//            业务代码开始
            int successCount = bookService.insert(book);
            book.setName("收拾收拾");
            if (successCount != 1) {
                return new Result(Code.SAVE_NO_OK, Code.SAVE_NO_OK_MSG, book);
            }


            return new Result(Code.SAVE_OK, Code.SAVE_OK_MSG, book);
//            业务代码开始结束

        }catch (Exception es){

            return null;
        }
//        try catch是每个controller方法中的公共部分，用来做异常捕获
    }

    //,produces="text/html;charset=UTF-8"
    //    RESTful修改 则URL仅仅是模块名 用什么请求方式做区分 put
//    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    @PutMapping
    public Result update(@RequestBody Book book) {
        int successCount = bookService.update(book);
        if (successCount != 1) {
            new Result(Code.SAVE_NO_OK, Code.SAVE_NO_OK_MSG, book.getId());
        }
        return new Result(Code.UPDATE_OK, Code.UPDATE_OK_MSG, book.getId());
    }
//    另一种
//    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
//    @ResponseBody
//    public Result update(@RequestBody Book book, @PathVariable("id") Integer id) {
//        int successCount = bookService.update(book);
//        if (successCount != 1) {
//            new Result(Code.SAVE_NO_OK, Code.SAVE_NO_OK_MSG, book.getId());
//        }
//        return new Result(Code.SAVE_OK, Code.SAVE_OK_MSG, book.getId());
//    }

    // 修改 ID放在问号后，作为URL参数传过来
    @RequestMapping("/updateById")
    @ResponseBody
    public String updateById(@RequestBody Book book, int id) {
        book.setId(id);
        int successCount = bookService.update(book);
        if (successCount != 1) {
            return "fail";
        }
        return "success";
    }

    // 修改 ID放在问号后，作为URL路径参数传过来
    @RequestMapping("/update/by/{id}")
    @ResponseBody
    public String updateByIdInPath(@RequestBody Book book, @PathVariable("id") int id) {
        book.setId(id);
        int successCount = bookService.update(book);
        if (successCount != 1) {
            return "fail";
        }
        return "成功";
    }

    //    URL http://localhost/update/status/by/id
//    修改请求方式 put
    @RequestMapping(value = "/{id}/{status}", method = RequestMethod.PUT)
    @ResponseBody
    public String updateByIdInPath(@PathVariable("status") String status,
                                   @PathVariable("id") int id) {

        int successCount = bookService.updateStatusById(status, id);
        if (successCount != 1) {
            return "fail";
        }
        return "成功";
    }

    //    getById
//    请求方式 get
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResultMap<Book> getByIdInPath(@PathVariable("id") int id) {
        if (id == -1){
            throw new BusinessException(Code.GET_BY_ID_NO_OK, Code.GET_BY_ID_NO_OK_MSG);
        }
        return new ResultMap<Book>(Code.GET_BY_ID_OK, Code.GET_BY_ID_OK_MSG, bookService.getById(id));
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id){
        bookService.delete(id);
        return new Result(Code.DELETE_OK, Code.DELETE_OK_MSG, null);
    }

    @GetMapping
    public Result getAll(){
        return new Result(Code.GET_ALL_OK, Code.GET_ALL_OK_MSG, bookService.getAll());
    }

}

